<template>
  <div>

      <div style="margin-bottom:15px">
        
        <el-button type="primary" style="margin-left:10px" @click="add()" v-if="user.role === 'ROLE_ADMIN'">添加</el-button>
      </div>
      <div>

        <el-table :data="tableData" style="width: 100%">
          


        <el-table-column prop="name" label="上传人" width="80px"></el-table-column>

        <el-table-column prop="subject" label="今日主题" width="80px"></el-table-column>


        <el-table-column prop="video" label="手术视频" align="center">
            <template slot-scope="{ row }">
              
              <video 
                width="1100px" 
                height="700px" 
                controls 
                style="border: none; cursor: pointer"
              >
                <source :src="row.video" type="video/mp4">
                
              </video>
            </template>
          </el-table-column>

          
          <el-table-column  label="操作" width="150px" v-if="user.role === 'ROLE_ADMIN'">
            <template slot-scope="scope">
              
              <el-button type="danger" @click="del(scope.row.id)"  style="margin-left:5px">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

            <div style="margin-top: 10px;">
          
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
              <div>

                    <el-dialog title="请上传视频" :visible.sync="dialogFormVisible" width="60%">
            <el-form :model="form">
              <el-form-item label="上传人" label-width="15%">
                <el-input v-model="form.name" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="今日主题" label-width="15%">
                <el-input v-model="form.subject" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="请在此上传视频" label-width="15%">
                <el-upload action="http://localhost:8080/api/files/upload" :on-success="successUpload">
                  <el-button size="small" type="primary">点击上传</el-button>            
                </el-upload>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取消</el-button>
              <el-button type="primary" @click="submit()">确定</el-button>
            </div>
          </el-dialog>

        </div>


  </div>
</template>

<script>
import request from '@/utils/request'
import { MessageBox, Message } from 'element-ui';



    export default {
      data() {
        return {
          params:{
            name:'',
            // category:'',
            pageNum:1,
            pageSize:5
          },
          tableData: [],
          total: 0,
          dialogFormVisible: false,
        
          form:{},
          // typeObjs:[],
          user:localStorage.getItem("user")?JSON.parse(localStorage.getItem("user")):{}
        }
      },
      //页面加载的时候，做一些事情，在created里面
      created(){
         this.findBySearch();
      },
      //定义一些页面上空间触发的事件调用的方法
      methods:{
        findBySearch(){
          request.get("/video/page",{
            params:this.params
          }).then(res =>{
            if(res.code === '0'){
              this.tableData = res.data.list
              this.total = res.data.total
            }else{
                  this.$message.error(res.msg)
            }
          })
        },
        add(){
          this.form = {}
          
          this.dialogFormVisible = true
        },
        
        handleSizeChange(pageSize){
           this.params.pageSize = pageSize
           this.findBySearch()
        },
        handleCurrentChange(pageNum){
          this.params.pageNum = pageNum
           this.findBySearch()
        },
        submit(){
        
          request.post("/video",this.form).then(res =>{
            if(res.code === '0'){
                this.$message.success("上传成功")
                this.dialogFormVisible = false
                this.findBySearch()
            }else{
                this.$message.error(res.msg)
            }
          })
        },
        del(id){

            MessageBox.confirm('确定删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                request.delete("/video/"+id).then(res =>{
              if(res.code === '0'){
                this.$message.success("删除成功")
              
                this.findBySearch()
              }else{
                this.$message.error(res.msg)
              }
            })
                // 这里放删除的逻辑
                // Message.success('删除成功');
              }).catch(() => {
                // 用户取消删除
                Message.info('取消删除');
              });

        },
        successUpload(res){
           console.log(res);
          //  this.form.img = res.data;
           this.form.video = res.data;
        }
       

      }
    }
  </script>